package com.fzshang.shopping.api.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fzshang.shopping.commen.bean.*;
import com.fzshang.shopping.commen.bean.layuiBean.ResultPage;
import com.fzshang.shopping.commen.bean.layuiBean.VoPage;
import com.fzshang.shopping.commen.controller.BaseController;
import com.fzshang.shopping.commen.entity.ApiResult;
import com.fzshang.shopping.commen.service.GoodService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 商品 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2020-03-08
 */
@Controller
@RequestMapping("admin/good")
public class GoodController extends BaseController {

    @RequestMapping(value = "good.html")
    public String good(Model model){
        model.addAttribute("commodityClassification",commodityClassificationService.list());
        model.addAttribute("classification",classificationService.list());
        return "admin/good/good";
    }
    @RequestMapping(value = "good_add.html")
    public String good_add(Model model){
        model.addAttribute("commodityClassification",commodityClassificationService.list());
        return "admin/good/good_add";
    }
    @RequestMapping(value = "good_edit.html")
    public String good_edit(Model model,Long id){
        model.addAttribute("commodityClassification",commodityClassificationService.list());
        model.addAttribute("classification",classificationService.list());
        Good good = goodService.getById(id);
        model.addAttribute("good",good);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("good_id",id);
        model.addAttribute("goodImage",goodImageService.list(queryWrapper));
        QueryWrapper queryWrapper1 = new QueryWrapper();
        queryWrapper1.eq("good_id",id);
        model.addAttribute("goodDetails",goodDetailsService.list(queryWrapper1));
        return "admin/good/good_edit";
    }



    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Good> selectMyPage(VoPage voPage, Good good){
        return goodService.selectMyPage(voPage,good);
    }


    /**
     * @param voPage
     * @return 后台查询每日推荐
     */
    @RequestMapping(value = "selectMyPage1",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Good> selectMyPage1(VoPage voPage){
        return goodService.selectMyPage1(voPage);
    }

    /**
     * @param voPage
     * @return 查询今日必推
     */
    @RequestMapping(value = "selectMyPageQuality",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Good> selectMyPageQuality(VoPage voPage){
        return goodService.selectMyPageQuality(voPage);
    }


    /**
     * @return 添加
     */
    @RequestMapping(value = "save")
    @ResponseBody
    public ApiResult save(Good good,String prods){
        //去除空的  防止json转换失败
        prods= StrUtil.removeAll(prods,"[],");
        prods= StrUtil.removeAll(prods,",[]");
        List<GoodSize> goodSizes = JSONObject.parseArray(prods,GoodSize.class);
        //先把信息添加道商品表
        good.setCreateTime(System.currentTimeMillis());
        good.setUserId(getUserInfo().getId());
        good.setState(0);
        good.setRecommend(0);
        if(goodService.save(good)){
            //添加商品主图
            String[] str = good.getGoodImage().split(";");
            for (int i = 0; i < str.length; i++) {
                GoodImage goodImage = new GoodImage();
                goodImage.setUrl(str[i]);
                goodImage.setGoodId(good.getId());
                goodImage.setId(null);
                if(!goodImageService.save(goodImage)){
                    return ApiResult.fail();
                }
            }
            String[] str1 = good.getGoodDetails().split(";");
            for (int i = 0; i < str1.length; i++) {
                GoodDetails goodDetails = new GoodDetails();
                goodDetails.setUrl(str1[i]);
                goodDetails.setGoodId(good.getId());
                goodDetails.setId(null);
                if(!goodDetailsService.save(goodDetails)){
                    return ApiResult.fail();
                }
            }
            //添加规格表
            for (int i = 0; i < goodSizes.size(); i++) {
                goodSizes.get(i).setGoodId(good.getId());
                goodSizes.get(i).setSizeId(goodSizes.get(i).getSizeId());
                goodSizes.get(i).setColor(goodSizes.get(i).getColor());
            }
            if(!goodSizeService.saveBatch(goodSizes)){
                return ApiResult.fail();
            }
        }


        return ApiResult.ok();
    }

    /**
     * @param param
     * @param goodImage
     * @param goodDetails
     * @return 修改
     */
    @RequestMapping(value = "updateById")
    @ResponseBody
    public ApiResult updateById(String param,String goodImage,String goodDetails){
        Good good = JSONObject.parseObject(param, Good.class);
        good.setCreateTime(System.currentTimeMillis());
        if(goodService.updateById(good)){
            if(goodImage!=""){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("good_id",good.getId());
                List<GoodImage> goodImages = goodImageService.list(queryWrapper);
                for (int i = 0; i < goodImages.size(); i++) {
                    if(!goodImageService.removeById(goodImages.get(i).getId())){
                        return ApiResult.fail("删除失败");
                    }
                }
                String[] str = goodImage.split(";");
                for (int i = 0; i < str.length; i++) {
                    GoodImage goodImage1 = new GoodImage();
                    goodImage1.setGoodId(good.getId());
                    goodImage1.setUrl(str[i]);
                    goodImage1.setId(null);
                    if(!goodImageService.save(goodImage1)){
                        return ApiResult.fail("添加商品图片失败!");
                    }
                }
            }
            if(goodDetails!=""){
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("good_id",good.getId());
                List<GoodDetails> goodDetails2 = goodDetailsService.list(queryWrapper);
                for (int i = 0; i < goodDetails2.size(); i++) {
                    if(!goodDetailsService.removeById(goodDetails2.get(i).getId())){
                        return ApiResult.fail("删除失败");
                    }
                }
                String[] str1 = goodDetails.split(";");
                for (int i = 0; i < str1.length; i++) {
                    GoodDetails goodDetails1 = new GoodDetails();
                    goodDetails1.setGoodId(good.getId());
                    goodDetails1.setUrl(str1[i]);
                    goodDetails1.setId(null);
                    if(!goodDetailsService.save(goodDetails1)){
                        return ApiResult.fail("添加商品详情图片失败!");
                    }
                }
            }
        }
        return ApiResult.ok();
    }

    @RequestMapping(value = "deleteById")
    @ResponseBody
    public ApiResult deleteById(Long id){
        if(goodService.removeById(id)){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("good_id",id);
            List<GoodImage> goodImages = goodImageService.list(queryWrapper);
            for (int i = 0; i < goodImages.size(); i++) {
                if(!goodImageService.removeById(goodImages.get(i).getId())){
                    return ApiResult.fail("删除商品图片失败");
                }
            }
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("good_id",id);
            List<GoodDetails> goodDetails = goodDetailsService.list(queryWrapper1);
            for (int i = 0; i < goodDetails.size(); i++) {
                    if(!goodDetailsService.removeById(goodDetails.get(i).getId())){
                        return ApiResult.fail("删除商品详情失败");
                }
            }
        }
        return ApiResult.ok();
    }


    /**
     * @param id
     * @return 今日必推
     */
    @RequestMapping(value = "today_we_must_push")
    @ResponseBody
    public ApiResult today_we_must_push(Long id){
        //先查询该商品是否上架 上架就在直接设置 没上架就提示
        QueryWrapper qq = new QueryWrapper();
        qq.eq("id",id);
        Good good3 = goodService.getOne(qq);
        if(good3.getBusinesses()!=1){
            return ApiResult.fail("该商品暂无上架!");
        }
        //先查询下商品中是否有今日必推产品
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",1);
        Good good = goodService.getOne(queryWrapper);
        //如果有就设置为不必推
        if(good!=null){
            //有必推,先把之前的设置为不必推产品,正在设置为必推
            Good good1 = new Good();
            good1.setId(good.getId());
            good1.setState(0);
            if(!goodService.updateById(good1)){
                return ApiResult.fail();
            }
            //再设置为必推产品
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",id);
            Good good2 = goodService.getOne(queryWrapper1);
            good2.setState(1);
            if(goodService.updateById(good2)){
                return ApiResult.ok();
            }
        }else{
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",id);
            Good good2 = goodService.getOne(queryWrapper1);
            good2.setState(1);
            if(goodService.updateById(good2)){
                return ApiResult.ok();
            }
        }
        return ApiResult.ok();
    }


    /**
     * @param id
     * @return 冰点秒杀
     */
    @RequestMapping(value = "freezing_point_seckill")
    @ResponseBody
    public ApiResult freezing_point_seckill(Long id){
        //先查询该商品是否上架 上架就在直接设置 没上架就提示
        QueryWrapper qq = new QueryWrapper();
        qq.eq("id",id);
        Good good3 = goodService.getOne(qq);
        if(good3.getBusinesses()!=1){
            return ApiResult.fail("该商品暂无上架!");
        }
        //先查询下商品中是否有今日必推产品
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",2);
        Good good = goodService.getOne(queryWrapper);
        //如果有就设置为不必推
        if(good!=null){
            //有必推,先把之前的设置为不必推产品,正在设置为必推
            Good good1 = new Good();
            good1.setId(good.getId());
            good1.setState(0);
            if(!goodService.updateById(good1)){
                return ApiResult.fail();
            }
            //再设置为必推产品
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",id);
            Good good2 = goodService.getOne(queryWrapper1);
            good2.setState(2);
            if(goodService.updateById(good2)){
                return ApiResult.ok();
            }
        }else{
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",id);
            Good good2 = goodService.getOne(queryWrapper1);
            good2.setState(2);
            if(goodService.updateById(good2)){
                return ApiResult.ok();
            }
        }
        return ApiResult.ok();
    }


    /**
     * @param id
     * @return 每日推荐
     */
    @RequestMapping(value = "recommend")
    @ResponseBody
    public ApiResult recommend(Long id){
        //先查询该商品是否上架 上架就在直接设置 没上架就提示
        QueryWrapper qq = new QueryWrapper();
        qq.eq("id",id);
        Good good3 = goodService.getOne(qq);
        if(good3.getBusinesses()!=1){
            return ApiResult.fail("该商品暂无上架!");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("recommend",1);
        List<Good> goods = goodService.list(queryWrapper);
        if(goods.size()==10){
            return ApiResult.fail("已经有十个商品!");
        }else{
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",id);
            Good good = goodService.getOne(queryWrapper1);
                if(good.getRecommend()==1){
                    return ApiResult.fail("该产品已经是每日推荐,不能再此设置!");
                }
                good.setRecommend(1);
                if(!goodService.updateById(good)){
                    return ApiResult.fail("添加失败");
                }
        }
        return ApiResult.ok();
    }
    /**
     * @param id
     * @return 每日推荐
     */
    @RequestMapping(value = "cancelRecommend")
    @ResponseBody
    public ApiResult cancelRecommend(Long id){
            QueryWrapper queryWrapper1 = new QueryWrapper();
            queryWrapper1.eq("id",id);
            Good good = goodService.getOne(queryWrapper1);
            good.setRecommend(0);
            if(!goodService.updateById(good)){
                return ApiResult.fail("添加失败");
            }
        return ApiResult.ok();
    }



}
